﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Drawing;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:SilverlightHost runat=server></{0}:SilverlightHost>")]
    public class SilverlightHost : System.Web.UI.WebControls.WebControl
    {
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        [Category("Silverlight")]
        public string ApplicationUrl
        {
            get
            {
                object obj = ViewState["ApplicationUrl"];
                if (obj == null) { return string.Empty; }
                return obj.ToString();
            }
            set { ViewState["ApplicationUrl"] = value; }
        }

        [Category("Silverlight")]
        public string ObjectId
        {
            get
            {
                object obj = ViewState["ObjectId"];
                if (obj == null) { return string.Empty; }
                return obj.ToString();
            }
            set { ViewState["ObjectId"] = value; }
        }

        [Category("Silverlight")]
        public string ObjectContext
        {
            get
            {
                object obj = ViewState["ObjectContext"];
                if (obj == null) { return string.Empty; }
                return obj.ToString();
            }
            set { ViewState["ObjectContext"] = value; }
        }

        [Category("Silverlight")]
        public Dictionary<string, string> Parameters
        {
            get
            {
                object obj = ViewState["Parameters"];
                if (obj == null) { return new Dictionary<string, string>(); }
                return (Dictionary<string, string>)obj;
            }
            set { ViewState["Parameters"] = value; }
        }

        [Category("Silverlight")]
        public Dictionary<string, string> ObjectEvents
        {
            get
            {
                object obj = ViewState["Events"];
                if (obj == null) { return new Dictionary<string, string>(); }
                return (Dictionary<string, string>)obj;
            }
            set { ViewState["Events"] = value; }
        }

        protected override void CreateChildControls()
        {
            LiteralControl script = new LiteralControl();
            script.ID = this.ID + "_Script";
            script.Text = GetJavaScript();
            this.Controls.Add(script);
        }

        private string GetJavaScript()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\" language=\"javascript\" >");
            sb.AppendLine("var getSilverlightMethodCall = \"javascript:Silverlight.getSilverlight(\\\"4.0.50401.0\\\");\"");
            sb.AppendLine("var installImageUrl = \"http://go.microsoft.com/fwlink/?LinkId=161376\";");
            sb.AppendLine("var imageAltText = \"Get Microsoft Silverlight\";");
            sb.AppendLine("var altHtml = \"<a href='{1}' style='text-decoration: none;'><img src='{2}' alt='{3}' style='border-style: none'/></a>\";");
            sb.AppendLine("altHtml = altHtml.replace('{1}', getSilverlightMethodCall);");
            sb.AppendLine("altHtml = altHtml.replace('{2}', installImageUrl);");
            sb.AppendLine("altHtml = altHtml.replace('{3}', imageAltText);");
            sb.AppendLine(string.Format("Silverlight.createObject(\"{0}\", {1}, \"{2}\", {{width: \"100%\", height: \"100%\", background: \"{6}\", alt: altHtml, version:\"4.0.50401.0\"}},{{ {3} }}, \"{4}\", \"{5}\");", this.ApplicationUrl, this.ClientID, this.ObjectId, GetSilverlightEvents(), GetParameters(), this.ObjectContext, ColorTranslator.ToHtml(this.BackColor)));
            sb.AppendLine("</script>");
            return sb.ToString();
        }

        private string GetSilverlightEvents()
        {
            StringBuilder sb = new StringBuilder();
            foreach (string name in ObjectEvents.Keys)
            {
                if (sb.Length > 0) { sb.Append(","); }
                sb.AppendFormat("{0}: {1}", name, ObjectEvents[name]);
            }

            return sb.ToString();
        }

        private string GetParameters()
        {
            StringBuilder sb = new StringBuilder();
            foreach (string name in Parameters.Keys)
            {
                if (sb.Length > 0) { sb.Append(","); }
                sb.AppendFormat("{0}={1}", name, Parameters[name]);
            }

            return sb.ToString();
        }
    }
}
